﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.ComponentModel;

namespace ChatTextBlockControl
{
    /// <summary>
    /// http://www.windowsphonegeek.com/articles/Creating-a-WP7-Custom-Control-in-7-Steps
    /// http://forums.silverlight.net/t/113667.aspx
    /// </summary>
    public class ChatTextBlock : Control
    {
        #region Properties
        public object Content
        {
            get { return (object)GetValue(ContentProperty); }
            set { SetValue(ContentProperty, value); }
        }

        public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(object), typeof(ChatTextBlock), null);

        [Description("Margin of content inside the balloon")]
        public Thickness ContentMargin
        {
            get { return (Thickness)GetValue(ContentMarginProperty); }
            set { SetValue(ContentMarginProperty, value); }
        }

        public static readonly DependencyProperty ContentMarginProperty =
            DependencyProperty.Register("ContentMargin", typeof(Thickness), typeof(ChatTextBlock), null);

        #region Timestamp
        public string TimestampText
        {
            get { return (string)GetValue(TimestampTextProperty); }
            set { SetValue(TimestampTextProperty, value); }
        }

        public static readonly DependencyProperty TimestampTextProperty =
            DependencyProperty.Register("TimestampText", typeof(string), typeof(ChatTextBlock), null);

        public Visibility TimestampVisibility
        {
            get { return (Visibility)GetValue(TimestampVisibilityProperty); }
            set { SetValue(TimestampVisibilityProperty, value); }
        }

        public static readonly DependencyProperty TimestampVisibilityProperty =
            DependencyProperty.Register("TimestampVisibility", typeof(Visibility), typeof(ChatTextBlock), null);

        public Style TimestampStyle
        {
            get { return (Style)GetValue(TimestampStyleProperty); }
            set { SetValue(TimestampStyleProperty, value); }
        }

        public static readonly DependencyProperty TimestampStyleProperty =
            DependencyProperty.Register("TimestampStyle", typeof(Style), typeof(ChatTextBlock), null);

        public double? TimestampFontSize
        {
            get { return (int)GetValue(TimestampFontSizeProperty); }
            set { SetValue(TimestampFontSizeProperty, value); }
        }

        public static readonly DependencyProperty TimestampFontSizeProperty =
            DependencyProperty.Register("TimestampFontSize", typeof(double?), typeof(ChatTextBlock), null);
        #endregion

        #region Arrow Visibility
        [Description("Visibility of top left arrow. Default collapsed.")]
        public Visibility TopLeftArrowVisibility
        {
            get { return (Visibility)GetValue(TopLeftArrowVisibilityProperty); }
            set { SetValue(TopLeftArrowVisibilityProperty, value); }
        }

        public static readonly DependencyProperty TopLeftArrowVisibilityProperty =
            DependencyProperty.Register("TopLeftArrowVisibility", typeof(Visibility), typeof(ChatTextBlock), null);

        [Description("Visibility of top right arrow. Default collapsed.")]
        public Visibility TopRightArrowVisibility
        {
            get { return (Visibility)GetValue(TopRightArrowVisibilityProperty); }
            set { SetValue(TopRightArrowVisibilityProperty, value); }
        }

        public static readonly DependencyProperty TopRightArrowVisibilityProperty =
            DependencyProperty.Register("TopRightArrowVisibility", typeof(Visibility), typeof(ChatTextBlock), null);

        [Description("Visibility of bottom left arrow. Default collapsed.")]
        public Visibility BottomLeftArrowVisibility
        {
            get { return (Visibility)GetValue(BottomLeftArrowVisibilityProperty); }
            set { SetValue(BottomLeftArrowVisibilityProperty, value); }
        }

        public static readonly DependencyProperty BottomLeftArrowVisibilityProperty =
            DependencyProperty.Register("BottomLeftArrowVisibility", typeof(Visibility), typeof(ChatTextBlock), null);

        [Description("Visibility of bottom right arrow. Default collapsed.")]
        public Visibility BottomRightArrowVisibility
        {
            get { return (Visibility)GetValue(BottomRightArrowVisibilityProperty); }
            set { SetValue(BottomRightArrowVisibilityProperty, value); }
        }

        public static readonly DependencyProperty BottomRightArrowVisibilityProperty =
            DependencyProperty.Register("BottomRightArrowVisibility", typeof(Visibility), typeof(ChatTextBlock), null);
        #endregion

        #endregion

        public ChatTextBlock() 
        {
            DefaultStyleKey = typeof(ChatTextBlock);
        }

        // set layout logic by handling MeasureOverride/ArrangeOverride. 
        protected override Size ArrangeOverride(Size finalSize)
        {
            return base.ArrangeOverride(finalSize);
        }
        protected override Size MeasureOverride(Size availableSize)
        {
            return base.MeasureOverride(availableSize);
        }
    }
}
